package cn.smxy.category.controller;

import cn.smxy.category.service.CategoryService;
import cn.smxy.param.PageParam;
import cn.smxy.pojo.Category;
import cn.smxy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 查看种群列表接口
     *
     * @return
     */
    @GetMapping("/list")
    public List<Category> list() {
        return categoryService.list();
    }

    /**
     * 添加种群接口
     *
     * @param category 种群实体类
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody @Validated Category category, BindingResult result) {
        if(result.hasErrors()) {
            return R.fail("参数为空，添加失败");
        }
        return categoryService.add(category);
    }

    // TODO: 管理员模块

    /**
     * 分页查看种群列表接口
     *
     * @return
     */
    @PostMapping("/page")
    public R page(@RequestBody PageParam pageParam) {
        return categoryService.page(pageParam);
    }

    /**
     * 修改种群接口
     * @param category
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody Category category) {
        return categoryService.update(category);
    }

    /**
     * 删除种群接口
     * @param categoryId
     * @return
     */
    @DeleteMapping("/remove/{categoryId}")
    public R remove(@PathVariable Long categoryId) {
        return categoryService.remove(categoryId);
    }

    /**
     * 获取类群信息接口
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/info/{categoryId}")
    public Category getInfo(@PathVariable Long categoryId) {
        return categoryService.getInfo(categoryId);
    }
}
